package com.youlu.campus.web.vote.controller;

import com.youlu.campus.base.base.PageQueryRequest;
import com.youlu.campus.entity.UserActivityFootPrint;
import com.youlu.campus.entity.UserActivityTaskInfo;
import com.youlu.campus.entity.UserInfoExtend;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.VO.req.UserActivityFootPrintQueryVO;
import com.youlu.campus.entity.course.group.Info;
import com.youlu.campus.entity.vote.vo.VoteActivityTaskUserInfoVo;
import com.youlu.campus.entity.vote.vo.VoteActivityUserInfoVo;
import com.youlu.campus.service.activity.ActivityFootPrintService;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.ranking.UserActivityPointDetailService;
import com.youlu.campus.service.visitor.UserInfoExtendService;
import com.youlu.campus.service.vote.ActivityVoteService;
import com.youlu.campus.service.vote.ActivityVoteTaskService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 投票活动获取票数
 */
@Slf4j
@RestController
@RequestMapping("/mini-app/")
public class ActivityVoteMyController {
    @Autowired
    private ActivityVoteTaskService activityVoteTaskService;
    @Autowired
    private UserActivityPointDetailService userActivityPointDetailService;
    @Autowired
    private ActivityFootPrintService activityFootPrintService;
    @Autowired
    private ActivityVoteService activityVoteService;
    @Autowired
    private UserInfoExtendService userInfoExtendService;
    @Autowired
    private ActivityService activityService;

    /**
     * 投票获取票数
     * 参数：request - 任务列表
     */
    @PostMapping("/vote/tasks")
    public QResult getVoteTasks(@Validated(Info.class) @RequestBody VoteActivityUserInfoVo request) {
        log.info(":>>> 投票活动任务列表: 活动id{}", request.getActivityId());

        UserPO userPO = RequestContext.getCurUser();
        UserInfoExtend userInfoExtend = userInfoExtendService.findByUserIdAndAppId(userPO);
        if(Objects.nonNull(userInfoExtend)) {
            userPO.setUniversityId(userInfoExtend.getUniversityId());
            userPO.setUniversityName(userInfoExtend.getUniversityName());
            userPO.setMobile(userInfoExtend.getMobile());
            userPO.setName(userInfoExtend.getName());
            request.setUserInfoComplete(userInfoExtend.getUserInfoComplete());
        }
        request.setUserPO(userPO);
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setActivityId(request.getActivityId());
        return QResult.success(activityVoteTaskService.getTaskInfoByUser(request));
    }

    /**
     * 添加活动
     * 参数：request - 任务列表
     */
    @PostMapping("/vote/create-task")
    public QResult createVoteTasks(@RequestBody UserActivityTaskInfo request) {
        log.info(":>>> 投票活动创建任务: 活动id{},活动类型{},任务id{}", request.getActivityId(), request.getTaskBizType(),request.getTaskId());
        request.setUserId(RequestContext.getCurUser().getId());
        return QResult.success(activityVoteTaskService.createTask(request));
    }

    /**
     * 领取活动票数
     * 参数：request - 领取活动票数
     */
    @PostMapping("/vote/receive-vote")
    public QResult receiveVoteTasks(@RequestBody VoteActivityTaskUserInfoVo userTaskId) {
        log.info(":>>> 投票活动任务完成领取票数: id{}", userTaskId);
        boolean re = activityVoteTaskService.receiveTaskVote(userTaskId);
        if(!re){
            return QResult.fail("领取失败！");
        }
        return QResult.success(true);
    }

    /**
     * 投票获取明细
     * 参数：request - 投票明细
     */
    @PostMapping("/vote/receive-record")
    public QResult voteLog(@RequestBody PageQueryRequest request){
        return QResult.success(userActivityPointDetailService.list(RequestContext.getUserId(),request));
    }

    /**
     * 投票证书领取
     * 参数：request - 投票证书领取
     */
    @PostMapping("/vote/receive-voteca")
    public QResult receiveVoteCa(@Validated(Info.class) @RequestBody VoteActivityUserInfoVo request){
        request.setUserPO(RequestContext.getCurUser());
        Pair<Boolean, Integer> res = activityVoteService.receiveVoteCa(request.getActivityId(), request.getUserPO().getId());
        if(res.getLeft()) {
            return QResult.success(res.getLeft());
        }else {
            return QResult.fail(res.getRight(),"领取失败");
        }
    }

    /**
     * 邀请证书领取
     * 参数：request - 邀请证书领取
     */
    @PostMapping("/vote/receive-inviteca")
    public QResult receiveInviteCa(@Validated(Info.class) @RequestBody VoteActivityUserInfoVo request){
        request.setUserPO(RequestContext.getCurUser());
        Pair<Boolean, Integer> res = activityVoteService.receiveInViteCa(request.getActivityId(), request.getUserPO().getId());
        if(res.getLeft()) {
            return QResult.success(res.getLeft());
        }else {
            return QResult.fail(res.getRight(),"领取失败");
        }
    }

    /**
     * 添加足迹列表
     * 参数：req - 添加足迹列表
     */
    @PostMapping("/vote/share-upload")
    public QResult createFootPrintUpload(@RequestBody UserActivityFootPrint req) {
        req.setUserId(RequestContext.getUserId());
        req.setUserPO(RequestContext.getCurUser());
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        return QResult.success(activityFootPrintService.createUserActivityTaskFootPrint(req));
    }

    /**
     * 查询用户足迹
     * 参数：req - 足迹列表查询
     */
    @PostMapping("/vote/share-info")
    public QResult getFootPrint(@RequestBody UserActivityFootPrintQueryVO req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(activityFootPrintService.getUserActivityFootPrintByTaskId(req));
    }

    /**
     * 删除足迹
     * 参数：req - 删除足迹
     */
    @PostMapping("/vote/share-delete")
    public QResult deleteFootPrint(@RequestBody UserActivityFootPrint req) {
        req.setUserId(RequestContext.getUserId());
        return QResult.success(activityFootPrintService.deleteUserActivityTaskFootPrint(req));
    }

    /**
     * 获取任务详情
     */
    @GetMapping("/vote/activity/task-detail")
    public QResult getTaskDetail(@RequestParam("taskId") String taskId) {
        return QResult.success(activityService.findActivityTaskInfo(taskId));
    }

}